import os
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import chat_agent_executor

# langsmith 监控

os.environ['LANGCHAIN_TRACING_V2'] = "true"
os.environ['LANGCHAIN_API_KEY'] = '1123'
os.environ['TAVILY_API_KEY'] = '1123111'

# 调用大模型

model = ChatOpenAI(model='gpt-4-turbo')

search = TavilySearchResults(max_results=2)  # 返回2个结果
search.invoke('HOW IS BEIJING TEMPRATURE?')

# 需要tavilysearch apikey,让模型绑定工具
model_with_tools = model.bind_tools([search])
resp = model_with_tools.invoke([HumanMessage(content='hello, everybody')])
# 模型可以自动推理，是否应该调用工具去完成用户的答案

tools = [search]
# 创建代理
agent_executor = chat_agent_executor.create_tool_calling_executor(model, tools)
resp = agent_executor.invoke({
    'messages': [HumanMessage(content='hello, what is the capital of China')]
})

print(resp['messages'])

resp2 = agent_executor.invoke({
    'messages': [HumanMessage(content='hello, what is the temprature of Beijing')]
})

print(resp2['messages'])
